package com.ucode.hrm.controller;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import com.ucode.hrm.service.HrmSocialSecurityRuleService;
import com.ucode.model.hrm.HrmSocialSecurityRule;
import com.ucode.model.hrm.convert.HrmSocialSecurityRuleConvert;
import com.ucode.model.hrm.form.HrmSocialSecurityRuleForm;
import com.ucode.tool.base.Paginator;
import com.ucode.tool.base.ResponseResult;
import com.ucode.tool.util.IdentifyUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.Size;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Tag(name = "管理后台 - 人力资源系统 - 社保规则")
@RestController
@RequestMapping("/hrm/social-security-rule")
@Validated
public class HrmSocialSecurityRuleController {

    @Resource
    private HrmSocialSecurityRuleService socialSecurityRuleService;

    @GetMapping("/page-list")
    @Operation(summary = "获得分页列表")
    @PreAuthorize("@usfs.hasPermission('hrm:social-security-rule:query')")
    public ResponseResult<Paginator<HrmSocialSecurityRule>> pageList(
            @Schema(description = "名称") @RequestParam(value = "name",required = false) String name,
            @Schema(description = "使用地区") @RequestParam(value = "useRegion",required = false) String useRegion,
            @Schema(description = "生效时间区间",example = "[2022-07-01 00:00:00,2022-07-01 23:59:59]")  @RequestParam(value = "createTimeInterval",required = false) LocalDateTime[] createTimeInterval,
            @Schema(description = "页码",example="1") @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @Schema(description = "每页记录数",example="5") @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize){

        Map<String,Object> condition = new HashMap<>();
        if(StrUtil.isNotBlank(name))
            condition.put("name", StrUtil.concat(true,"%", name,"%"));
        if(StrUtil.isNotBlank(useRegion))
            condition.put("useRegion", StrUtil.concat(true,"%", useRegion,"%"));
        if(ArrayUtil.isNotEmpty(createTimeInterval) && createTimeInterval.length == 2){
            condition.put("startTime",createTimeInterval[0]);
            condition.put("endTime",createTimeInterval[1]);
        }
        condition.put("deltag",false);

        Paginator<HrmSocialSecurityRule> paginator = socialSecurityRuleService.queryPageForList(condition,pageSize,pageNum);
        return ResponseResult.success(paginator);
    }

    @GetMapping("/list-simple-page")
    @Operation(summary = "分页获取精简信息列表(没有权限校验，公共查询接口)", description = "主要用于前端的下拉选项")
    public ResponseResult<Paginator<HrmSocialSecurityRule>> simplePageList(
                                                                      @Schema(description = "名称") @RequestParam(value = "name",required = false) String name,
                                                                      @Schema(description = "使用地区") @RequestParam(value = "useRegion",required = false) String useRegion,
                                                                      @Schema(description = "页码",example="1") @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                                      @Schema(description = "每页记录数",example="5") @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize) {
        Map<String,Object> condition = new HashMap<>();
        if(StrUtil.isNotBlank(name))
            condition.put("name", StrUtil.concat(true,"%", name,"%"));
        if(StrUtil.isNotBlank(useRegion))
            condition.put("useRegion", StrUtil.concat(true,"%", useRegion,"%"));
        condition.put("deltag",false);

        Paginator<HrmSocialSecurityRule> paginator = socialSecurityRuleService.queryPageForList(condition,pageSize,pageNum);
        return ResponseResult.success(paginator);
    }

    @GetMapping("/getById/{id}")
    @Parameter(description="主键ID",name = "id", required = true,in = ParameterIn.PATH,example = "1024")
    @Operation(summary = "主键获得信息")
    @PreAuthorize("@usfs.hasPermission('hrm:social-security-rule:query')")
    public ResponseResult<HrmSocialSecurityRule> getById(@PathVariable(name="id",required=true) Long id){
        HrmSocialSecurityRule bean = socialSecurityRuleService.validate(id);
        return ResponseResult.success(bean);
    }

    @PostMapping("/create")
    @Operation(summary = "创建")
    @PreAuthorize("@usfs.hasPermission('hrm:social-security-rule:create')")
    public ResponseResult<Long> create(@Valid @RequestBody HrmSocialSecurityRuleForm form) {
        HrmSocialSecurityRule bean = HrmSocialSecurityRuleConvert.INSTANCE.convert(form);
        bean.setId(IdentifyUtils.getDefaultSnowflakeId());
        Long id = socialSecurityRuleService.insert(bean);
        return id != null ? ResponseResult.success(id) : ResponseResult.failed();
    }

    @PostMapping("/update/{id}")
    @Parameter(description="主键ID",name = "id", required = true,in = ParameterIn.PATH,example = "1024")
    @Operation(summary = "修改")
    @PreAuthorize("@usfs.hasPermission('hrm:social-security-rule:update')")
    public ResponseResult<Object> update(@PathVariable(name="id",required=true) Long id,
                                         @Valid @RequestBody HrmSocialSecurityRuleForm form) {
        HrmSocialSecurityRule bean = HrmSocialSecurityRuleConvert.INSTANCE.convert(form);
        bean.setId(id);
        int i = socialSecurityRuleService.update(bean);
        return i > 0 ? ResponseResult.success() : ResponseResult.failed();
    }

    @PostMapping("/logic-delete/{id}")
    @Operation(summary = "转移到回收站")
    @Parameter(description="主键ID",name = "id", required = true,in = ParameterIn.PATH, example = "1024")
    @PreAuthorize("@usfs.hasPermission('hrm:social-security-rule:logic-delete')")
    public ResponseResult<Object> logicDelete(@PathVariable(name="id",required=true) Long id) {
        //逻辑删除
        int i = socialSecurityRuleService.logicDeletePrimaryKey(id);
        return i > 0 ? ResponseResult.success() : ResponseResult.failed();
    }

    @PostMapping("/batch-logic-delete")
    @Operation(summary = "批量转移到回收站")
    @PreAuthorize("@usfs.hasPermission('hrm:social-security-rule:logic-delete')")
    public ResponseResult<Boolean> batchLogicDeletePrimaryKey(@Schema(description = "编号集合",requiredMode = Schema.RequiredMode.REQUIRED)
                                                @NotEmpty(message = "编号集合为空")@Size(min = 1,max = 100,message = "编号集合数量为1-100条")
                                                @RequestParam(value = "ids",required = true) List<Long> ids) {
        int i = socialSecurityRuleService.logicDeleteByPrimaryKeys(ids);
        return i > 0 ? ResponseResult.success() : ResponseResult.failed();
    }

}
